<template>
	<view class="page">
		<!-- <view class="flex row-center m-b-80 m-t-80">
			<uqrcode ref="uqrcode" canvas-id="qrcode" :value="value" :options="{ margin: 10 }"></uqrcode>
		</view>
		<view @click="save" class="bold font-size-36 text-center">分享二维码给好友，获取积分</view> -->
		<view class="flex row-center">
			<image :show-menu-by-longpress="true" class="img" :src="tjm" mode=""></image>
		</view>

		<view @click="saveImage" class="bold font-size-36 text-center">分享二维码给好友，获取积分</view>
	</view>
</template>

<script>
// import UQRCode from '@/uni_modules/Sansnn-uQRCode/js_sdk/uqrcode/uqrcode.js';
const app = getApp();
export default {
	data() {
		return {
			value: '',
			tjm: app.globalData.tjm
		};
	},
	// onReady() {
	// 	this.value = 'https://uniapp.dcloud.net.cn/';
	// 	this.$refs.uqrcode.toTempFilePath({
	// 		success: (res) => {
	// 			console.log(res);
	// 		}
	// 	});
	// },
	methods: {
		// save() {
		// 	console.log('保存');
		// 	// uqrcode为组件的ref名称
		// 	this.$refs.uqrcode.save({
		// 		success: () => {
		// 			uni.showToast({
		// 				icon: 'success',
		// 				title: '保存成功'
		// 			});
		// 		}
		// 	});
		// },
		saveImage() {
			// 创建下载任务
			const downloadTask = uni.downloadFile({
				url: this.tjm,
				success: (downloadResult) => {
					if (downloadResult.statusCode === 200) {
						// 下载成功，保存图片到系统相册
						uni.saveImageToPhotosAlbum({
							filePath: downloadResult.tempFilePath,
							success: () => {
								uni.showToast({ title: '图片保存成功' });
							},
							fail: () => {
								uni.showToast({ title: '图片保存失败', icon: 'none' });
							}
						});
					}
				},
				fail: () => {
					uni.showToast({ title: '图片下载失败', icon: 'none' });
				}
			});

			// 显示下载提示
			downloadTask.onProgressUpdate((res) => {
				console.log('下载进度' + res.progress);
				console.log('已经下载的数据长度' + res.totalBytesWritten);
				console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.page {
	padding: 10rpx;

	.img {
		width: 260rpx;
		height: 260rpx;
		margin: 160rpx 0;
	}
}
</style>
